package cn.yunxiang.summer.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import cn.yunxiang.summer.common.domain.R;
import cn.yunxiang.summer.common.excel.utils.ExcelUtil;
import cn.yunxiang.summer.common.log.annotation.Log;
import cn.yunxiang.summer.common.log.enums.BusinessType;
import cn.yunxiang.summer.system.convert.SysTenantAccountConvert;
import cn.yunxiang.summer.system.domain.SysTenantAccount;
import cn.yunxiang.summer.system.domain.bo.SysTenantAccountBo;
import cn.yunxiang.summer.system.domain.vo.SysTenantAccountVo;
import cn.yunxiang.summer.system.service.ISysTenantAccountService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "租户账户管理接口")
@RestController
@RequestMapping("/system/tenant/account")
@RequiredArgsConstructor
public class SysTenantAccountController {

    private final ISysTenantAccountService service;
    private final SysTenantAccountConvert convert;

    @Operation(summary = "查询租户账户列表")
    @PostMapping("/list")
    public R<IPage<SysTenantAccountVo>> fansList(@RequestBody SysTenantAccountBo bo, Page<SysTenantAccount> page) {
        IPage<SysTenantAccountVo> result = service.selectTenantAccountPage(page, bo);
        return R.ok(result);
    }

    @Operation(summary = "导出租户账户列表")
    @Log(title = "租户账户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R<String> export(SysTenantAccountBo query) {
        List<SysTenantAccount> list = service.list(query);
        ExcelUtil<SysTenantAccountVo> util = new ExcelUtil<>(SysTenantAccountVo.class);
        return R.ok(util.writeExcel(convert.toVO(list), "租户账户数据"));
    }

    @Operation(summary = "获取租户账户详细信息")
    @GetMapping(value = "/{id}")
    public R<SysTenantAccountVo> getInfo(@Parameter(description = "租户账户ID") @PathVariable("id") Long id) {
        return R.ok(convert.toVO(service.getById(id)));
    }

    @Operation(summary = "新增租户账户")
    @Log(title = "租户账户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Object> add(@RequestBody SysTenantAccountBo tenantAccount) {
        return R.ok(service.save(convert.toPo(tenantAccount)));
    }

    @Operation(summary = "修改租户账户")
    @Log(title = "租户账户", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Object> edit(@RequestBody SysTenantAccountBo tenantAccount) {
        return R.ok(service.updateById(convert.toPo(tenantAccount)));
    }

    @Operation(summary = "删除租户账户")
    @Log(title = "租户账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Object> remove(@Parameter(description = "租户账户ID") @PathVariable Long id) {
        return R.ok(service.removeById(id));
    }
}
